﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Resources;
using System.Globalization;


namespace atm_qlda
{
    public partial class Login : Form
    {
 
        DataTable dt;
        SqlDataAdapter da;
        DataSet ds;
        CultureInfo culture;
        string error;
        string error1;
        string language;
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
          this.txtID.Focus();

        }
        public delegate void delegateSend(string text, string language);
 
        public void SetLanguage(string cultureName)
        {
            culture = CultureInfo.CreateSpecificCulture(cultureName);
            ResourceManager rm = new ResourceManager("atm_qlda.Lang.MyResource", typeof(Login).Assembly);
            lbAccount.Text = rm.GetString("acc", culture);
            lbPin.Text = rm.GetString("pin", culture);
            label1.Text = rm.GetString("bank", culture);
            lbClear.Text = rm.GetString("reset", culture);
            lbEnter.Text = rm.GetString("enter", culture);
            this.Text = rm.GetString("nameLogin", culture);
            error = rm.GetString("errorLogin",culture);
            error1 = rm.GetString("errorLogin2", culture);
   
        }
        

        private void button3_Click(object sender, EventArgs e)
        {
            String id = txtID.Text;
            String pin = txtPIN.Text;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
            dt = new DataTable();
            ds = new DataSet();
            String sql = "select * from Card, UserAcc where Card.IDCard = '" + id + "' and Card.PIN = '" + pin + "' and Card.IDCard=UserAcc.IDCard";
            da = new SqlDataAdapter(sql,conn);
            da.Fill(ds,"acc");
            dt = ds.Tables["acc"];
            if (dt.Rows.Count != 0)
            {
                DataRow dr = dt.Rows[0];
                string status=dr["Status"].ToString();
                if (status == "False")
                {
                    MessageBox.Show(error1);
                    Application.Exit();
                }
                else
                {
                    Choose rt = new Choose();
                    delegateSend dele = new delegateSend(rt.chuyendl);
                    dele(this.txtID.Text, this.language);
                    rt.Show();
                    rt.SetLanguage(language);
                    this.Hide();
                }
            }
            else{
             MessageBox.Show(error);
             txtID.Focus();
             txtPIN.Clear();
             txtID.Clear();
                 }
            conn.Close();
            }

        private void bttClear_Click(object sender, EventArgs e)
        {
            txtID.Focus();
            txtPIN.Clear();
            txtID.Clear();
        }

        private void txtID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txtPIN.Focus();
            }
        }

        public void sendnn(string text)
        {
            this.language = text;        
        }

        private void txtID_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            this.txtID.Focus();
            if (txtID.TextLength == 13)
                txtPIN.Focus();
        }
        
     }
}